require File.join(Rails.root, 'lib', 'cartasur_authenticated_system.rb')

class Cartasur::ApplicationController < ActionController::Base
  layout 'cartasur'

  protect_from_forgery 
  #TODO see http://stackoverflow.com/questions/6589522/rails-3-protect-from-forgery-not-doing-anything# See ActionController::RequestForgeryProtection for details


  helper :all # include all helpers, all the time

  rescue_from ActiveRecord::RecordNotFound, :with => :show_errors

  include ::CartasurAuthenticatedSystem

  before_filter :set_locale

  def show_errors
      render :partial => 'shared/record_not_found', :layout => 'application', :status => 404
  end

  def set_locale
    locale = params[:locale] || :en
    I18n.locale = locale
  end

  def get_browser_locale
    preferred_locale = nil
    if browser_locale = request.headers['HTTP_ACCEPT_LANGUAGE']
      preferred_locale = %w(en es fr de).
              select { |i| browser_locale.include?(i) }.
              collect { |i| [i, browser_locale.index(i)] }.
              sort { |a, b| a[1] <=> b[1] }.
              first
    end
    preferred_locale.try(:first) || 'en'
  end

  def get_location_by_ip
    ip=request.remote_ip

    #if Rails.env=="production"
    #  return G.look_up(ip)
    #else
      cityobj = G.city(ip)
      cityobj = G.city '194.7.114.2' if cityobj.blank?
    #end
    cityobj
  end


  def default_url_options(options={})
    logger.debug "default_url_options is passed options: #{options.inspect}\n"
    { :locale => I18n.locale }
  end

end
